pacman::p_load(tidyverse, readxl, lubridate)

setwd('put_your_wd_here')

media <- read_csv('datasets/mass-shooting-stories-over-time-20220829234356.csv',col_names = c(
  'date','count','total_count','ratio'
), skip=1)

shootings <- read_excel('datasets/additional_shootings.xlsx')
shootings$date <- ymd(shootings$date)
media$date <- ymd(media$date)

shootings$total_media <- NA 
shootings$total_media_count <- NA 

start_dates <- shootings$date
end_dates <- shootings$date + weeks(1)

for(i in 1:length(start_dates)){
  seq_out <- seq(start_dates[i],end_dates[i], by='day')
  shootings$total_media[i] <- sum(media$ratio[media$date %in% seq_out], na.rm=T)
  print(i)
}

shootings$xlab <- paste(shootings$shooting, ' (', format(ymd(shootings$date),format = "%m/%d/%Y"),')', sep='')
shootings <- shootings %>%
  arrange(desc(total_media)) %>%
  mutate(rank = c(
    rep(1,10),
    rep(2,10),
    rep(3,10),
    rep(4,10)
  ))
media_plot <- ggplot(shootings, aes(x=reorder(xlab,total_media), y=total_media)) +
  geom_bar(stat='identity', fill='white', color='black') +
  coord_flip() +
  labs(x='', y='Cumulative Media Coverage') +
  geom_hline(yintercept=median(shootings$total_media), color='red', linetype=2) +
  theme_bw() +
  facet_grid(rank ~ .,scales='free_y')

ggsave(media_plot, width=6, height=6, 
       filename = 'figures/media_plot.png')  

